import Player from "./Player";
import AngleUtils from "./Trigon/AngleUtils";
import { RandomUtils } from "./Trigon/Random";

const { ccclass, property } = cc._decorator;

export enum ZombieState {
	red,
	pink,
}
cc.Enum(ZombieState);

@ccclass
export default class Zombie extends cc.Component {
	@property(cc.Prefab)
	supplyPrefab: cc.Prefab = null;

	@property
	maxSpeed: number = 120;

	@property
	maxHealth = 3;

	@property({ type: ZombieState })
	state:ZombieState= ZombieState.red;

	_health: number;
	_speed: number;

	onLoad() {
        switch(this.state){
            case ZombieState.red:
                this.node.color=cc.Color.RED;
                this._speed = RandomUtils.rangeInt(40, this.maxSpeed);
                break;
            case ZombieState.pink:
                this.node.color=cc.Color.fromHEX(this.node.color,'#FF97F5');
                this._speed = RandomUtils.rangeInt(80, this.maxSpeed);
            break;
        }
    }

	start() {
		this._health = RandomUtils.rangeInt(1, this.maxHealth);
	

	}

	die() {
		if (Math.random() < 0.2) {
			this.spawnSupply();
		}
		cc.game.emit("zombie-die", { a: 10 });
		this.node.destroy();
	}

	spawnSupply() {
		let supply = cc.instantiate(this.supplyPrefab);
		supply.getComponent("Supply").reset();
		this.node.parent.addChild(supply);
		supply.position = this.node.position;
	}

	moveToPos( playerPos: cc.Vec3) {
		let angle = AngleUtils.getAngle(this.node.position, playerPos)
		let v2 = AngleUtils.conventToVec2(angle, this._speed);
		let rigidBody = this.node.getComponent(cc.RigidBody);
		rigidBody.linearVelocity = v2;
        return v2;
	}


    anticipationPlayer(){
        let v=cc.v2();
        let player=Player.instance.node;
     
        //玩家与僵尸的距离
        let distance=cc.Vec2.distance(player.position,this.node.position);
        //僵尸移动到玩家位置需要的时间
        let time=distance/this._speed;
        let playerRigiBody=player.getComponent(cc.RigidBody);
        let playerVel=playerRigiBody.linearVelocity;
        //预判玩家位置
        let anticipation=player.position.add(cc.v3(playerVel.div(time)));
        //僵尸向预判的位置
        let zv=this.moveToPos(anticipation);
        //玩家移动和僵尸移动的交点
        let line1=player.getPosition().add(playerVel.mul(time))
        let line2=player.getPosition().add(zv.mul(time));
        if(AngleUtils.isCross(player.getPosition(),line1,this.node.getPosition(),line2)){
            let cross=AngleUtils.crossPoint(player.getPosition(),line1,this.node.getPosition(),line2);        
            this.moveToPos(cc.v3(cross));
        }
       
    }

	update(dt) {
		let player = Player.instance.node;
		switch (this.state) {
			case ZombieState.red:
				this.moveToPos( player.position);
				break;

			case ZombieState.pink:
                this.anticipationPlayer();
				break;
		}
	}

	hurt() {
		this._health--;
		if (this._health <= 0) {
			this.die();
		}

        let back=this.node.color
		cc.tween(this.node)
			.to(0.25, { color: cc.color(200, 200, 200) })
			.to(0.25, { color: back })
			.start();
	}

	onBeginContact(contact, self: cc.Collider, other: cc.Collider) {
		if (other.node.group == "bullet") {
			this.hurt();
		}
	}
}
